/*
 * @Author: sjr
 * @Date: 2021-01-31 15:59:21
 * @LastEditTime: 2021-01-31 16:10:41
 * @LastEditors: sjr
 * @Description: 描述
 * @FilePath: \leetcode\107.二叉树的层序遍历-ii.js
 */
/*
 * @lc app=leetcode.cn id=107 lang=javascript
 *
 * [107] 二叉树的层序遍历 II
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrderBottom = function(root) {
  var queue = []
  var res = []
  if(root) queue.push(root)
  while(queue.length){
    let curRes = []
    let len = queue.length
    for (let i = 0; i < len; i++){
      let node = queue.shift()
      curRes.push(node.val)
      if(node.left) queue.push(node.left)
      if(node.right) queue.push(node.right)
    }
    res.unshift(curRes)
  }
  return res
};
// @lc code=end

/*
  var res = []
  const levelOrder = (node, level)=>{
    if(!node) return null
    res[level] = res[level] || []
    res[level].push(node.val)
    levelOrder(node.left, level+1)
    levelOrder(node.right, level+1)
  }
  levelOrder(root,0)
  return res.reverse()
*/